//
//  MineViewController.swift
//  TalkeTv
//
//  Created by lhf on 2024/10/19.
//

import UIKit

class MineViewController: BaseViewController {

    @IBOutlet weak var userAccount: UILabel!
    @IBOutlet weak var userAvatar: UIImageView!
    
    @IBOutlet weak var menuBgView: UIView!
    private var menuList:[[String: String]] = [
        ["icon": "history", "title": "查看历史"],
        ["icon": "changePass", "title": "修改密码"],
        ["icon": "changeAvatar", "title": "更换头像"],
        ["icon": "logout", "title": "退出登录"],
    ]
    private let menuScrollView: UIScrollView = UIScrollView.init()
    
    // MARK: 加载功能按钮
    func handleReloadMenuView() {
        for view in menuScrollView.subviews {
            view.removeFromSuperview()
        }
        
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.masksToBounds = true
        bgView.layer.cornerRadius = 8
        menuScrollView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
            make.width.equalTo(menuBgView.snp.width)
        }
        
        let itemHeight = 52.0
        for index in 0..<menuList.count {
            let item = menuList[index]
            
            let itemBgView = UIView.init()
            bgView.addSubview(itemBgView)
            if index == 0 {
                itemBgView.snp.makeConstraints { (make) in
                    make.left.top.right.equalToSuperview()
                    make.height.equalTo(itemHeight)
                }
            } else if index == menuList.count - 1 {
                itemBgView.snp.makeConstraints { (make) in
                    make.left.right.equalToSuperview()
                    make.top.equalTo(bgView.subviews[index - 1].snp.bottom)
                    make.bottom.equalToSuperview()
                    make.height.equalTo(bgView.subviews[index - 1].snp.height)
                }
            } else {
                itemBgView.snp.makeConstraints { (make) in
                    make.left.right.equalToSuperview()
                    make.top.equalTo(bgView.subviews[index - 1].snp.bottom)
                    make.height.equalTo(bgView.subviews[index - 1].snp.height)
                }
            }
            
            let icon = UIImageView.init()
            icon.image = UIImage(named: item["icon"]!)
            itemBgView.addSubview(icon)
            icon.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(16)
                make.centerY.equalToSuperview()
                make.width.height.equalTo(16)
            }
            
            let rightArrow = UIImageView.init()
            rightArrow.image = UIImage(named: "right_arrow")
            itemBgView.addSubview(rightArrow)
            rightArrow.snp.makeConstraints { make in
                make.right.equalToSuperview().offset(-16)
                make.centerY.equalToSuperview()
                make.height.equalTo(12)
            }
            
            let title = UILabel.init()
            title.text = item["title"]
            title.font = UIFont.systemFont(ofSize: 14)
            title.textColor = AppConfig.sharedInstance.MAIN_TITLE_COLOR
            itemBgView.addSubview(title)
            title.snp.makeConstraints { make in
                make.left.equalTo(icon.snp.right).offset(8)
                make.centerY.equalToSuperview()
                make.top.bottom.equalToSuperview()
            }
            
            let bottomLine = UIView.init()
            bottomLine.backgroundColor = AppConfig.sharedInstance.MAIN_BG_VIEW_COLOR
            itemBgView.addSubview(bottomLine)
            bottomLine.snp.makeConstraints { make in
                make.left.equalToSuperview().offset(16)
                make.right.equalToSuperview().offset(-16)
                make.bottom.equalToSuperview()
                make.height.equalTo(1)
            }
            
            let btn = UIButton.init()
            btn.setTitle(item["title"], for: .reserved)
            btn.addTarget(self, action: #selector(handleClickMenu(_:)), for: .touchUpInside)
            itemBgView.addSubview(btn)
            btn.snp.makeConstraints { make in
                make.left.top.bottom.right.equalToSuperview()
            }
            
        }
    }
    
    @objc func handleClickMenu(_ btn: UIButton) {
        let title = btn.title(for: .reserved)
        if title == "查看历史" {
            let history = UtilManager.sharedInstance.getVCFromSB(identifier: "HistoryViewController") as! HistoryViewController
            self.navigationController?.pushViewController(history, animated: true)
        } else if title == "修改密码" {
            let changePass = UtilManager.sharedInstance.getVCFromSB(identifier: "ChangePasswordViewController") as! ChangePasswordViewController
            self.navigationController?.pushViewController(changePass, animated: true)
        }  else if title == "更换头像" {
            let changeAvatar = UtilManager.sharedInstance.getVCFromSB(identifier: "ChangeAvatarViewController") as! ChangeAvatarViewController
            self.navigationController?.pushViewController(changeAvatar, animated: true)
        } else if title == "退出登录" {
            UIAlertController.init().showAlert(title: "提示", message: "确定退出登录？") {
                UserInfo.sharedInstance.clearUserAutoLoginKey()
                UserInfo.sharedInstance.toLogin("normal")
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        menuScrollView.showsVerticalScrollIndicator = false
        menuBgView.addSubview(menuScrollView)
        menuScrollView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        handleReloadMenuView()
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        if UserInfo.sharedInstance.isLogin() {
            userAccount.text = "\(UserInfo.sharedInstance.userName) | \(UserInfo.sharedInstance.userAccount)"
            if UserInfo.sharedInstance.userAvatar != "" {
                userAvatar.af.setImage(withURL: URL(string: UserInfo.sharedInstance.userAvatar)!, placeholderImage: UIImage.init(named: "logo"))
            } else {
                userAvatar.image = UIImage.init(named: "logo")
            }
        } else {
            userAvatar.image = UIImage.init(named: "logo")
            userAccount.text = "左邻美课"
        }        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
